/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.bus.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.thinkgem.jeesite.common.utils.EnumProperty;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.bus.entity.VaccineEnt;
import com.thinkgem.jeesite.modules.bus.service.VaccineEntService;
import com.thinkgem.jeesite.modules.sys.entity.Dict;
import com.thinkgem.jeesite.modules.sys.service.DictService;
import com.thinkgem.jeesite.modules.sys.utils.DictUtils;

/**
 * 疫苗厂家信息表Controller
 * @author sy
 * @version 2019-06-14
 */
@Controller
@RequestMapping(value = "${adminPath}/bus/vaccineEnt")
public class VaccineEntController extends BaseController {

	@Autowired
	private VaccineEntService vaccineEntService;
	@Autowired
	private DictService dictService;
	
	@ModelAttribute
	public VaccineEnt get(@RequestParam(required=false) String id) {
		VaccineEnt entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = vaccineEntService.get(id);
		}
		if (entity == null){
			entity = new VaccineEnt();
		}
		return entity;
	}
	
	@RequiresPermissions("bus:vaccineEnt:view")
	@RequestMapping(value = {"list", ""})
	public String list(VaccineEnt vaccineEnt, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<VaccineEnt> page = vaccineEntService.findPage(new Page<VaccineEnt>(request, response), vaccineEnt); 
		model.addAttribute("page", page);
		return "modules/bus/vaccineEntList";
	}

	@RequiresPermissions("bus:vaccineEnt:view")
	@RequestMapping(value = "form")
	public String form(VaccineEnt vaccineEnt, Model model) {
		Dict dict=new Dict();
		dict.setType("vaccineType");
		List<Dict> list=DictUtils.getDictList("vaccineType");
		if(StringUtils.isNotEmpty(vaccineEnt.getId())){
			dict.setValue(vaccineEnt.getVaccineTypeCode());
			List<Dict> checkList=dictService.findLabelByValue(dict);
			vaccineEnt.setDictList(checkList); 
		}
		model.addAttribute("list",list);
		model.addAttribute("vaccineEnt", vaccineEnt);
		return "modules/bus/vaccineEntForm";
	}


	@RequiresPermissions("bus:vaccineEnt:edit")
	@RequestMapping(value = "save")
	@ResponseBody
	public String save(VaccineEnt vaccineEnt, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, vaccineEnt)){
			return form(vaccineEnt, model);
		}
		if(StringUtils.isNotEmpty(vaccineEnt.getId())){
			String name=vaccineEntService.validateEditName(vaccineEnt.getId());
			if(name.equals(vaccineEnt.getName())){
				vaccineEntService.save(vaccineEnt);
				addMessage(redirectAttributes, "保存疫苗厂家信息成功");
				return EnumProperty.SaveEnum.SAVESUCCESS.getKey();
			}
		}
		int count=vaccineEntService.validateName(vaccineEnt.getName());
		if(count>0){
			addMessage(redirectAttributes, "厂家名称重复,保存失败！");
			return EnumProperty.SaveEnum.SAVEFAILURE.getKey();
		}else{
			vaccineEntService.save(vaccineEnt);
            addMessage(redirectAttributes, "保存疫苗厂家信息成功");
			return EnumProperty.SaveEnum.SAVESUCCESS.getKey();
		}
	//	return "redirect:"+Global.getAdminPath()+"/bus/vaccineEnt/?repage";
	}
	
	@RequiresPermissions("bus:vaccineEnt:edit")
	@RequestMapping(value = "delete")
	public String delete(VaccineEnt vaccineEnt, RedirectAttributes redirectAttributes) {
		vaccineEntService.delete(vaccineEnt);
		addMessage(redirectAttributes, "删除疫苗厂家信息成功");
		return "redirect:"+Global.getAdminPath()+"/bus/vaccineEnt/?repage";
	}

}